package com.controller;

import com.alibaba.fastjson.JSON;

import com.common.AjaxResult;
import com.common.RequestMapping;
import com.common.RequestMappingAnalysis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者: 谢韬元
 * 日期: 2024/12/14
 * 类名: LoginServlet
 */
@RequestMapping("/auth")
public class LoginServlet {

    @WebServlet(name = "DipatchServlet", urlPatterns = "/")
    public class DipatchServlet extends HttpServlet {

        Map<String, String> urlMapping = new HashMap<String, String>();

        @Override
        public void init() throws ServletException {
            super.init();
//        urlMapping.put("/cart/list", "org.liuqm.controller.CartServlet.getList");
//        urlMapping.put("/cart/add", "org.liuqm.controller.CartServlet.add");
//        urlMapping.put("/cart/del", "org.liuqm.controller.CartServlet.del");
//        urlMapping.put("/cart/update", "org.liuqm.controller.CartServlet.update");

            try {
                urlMapping.putAll(RequestMappingAnalysis.getRequestMapping("org.liuqm.controller"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            if (resp.getContentType() == null) {
                // 设置响应数据的内容类型为application/json;
                resp.setContentType("application/json;charset=UTF-8");
            }

            System.out.println("进入DipatchServlet");
            String urlStr = req.getRequestURI();
            //通过反射获取到class实例，再调用相应的方法
            String urlValue = urlMapping.get(urlStr);

            PrintWriter out = resp.getWriter();
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setCode(500);
            if (urlValue == null) {

                ajaxResult.setCode(404);
                ajaxResult.setMsg("您访问的路径不存在");
                out.print(JSON.toJSONString(ajaxResult));
            }
            String className = urlValue.substring(0, urlValue.lastIndexOf("."));
            String methodName = urlValue.substring(urlValue.lastIndexOf(".") + 1);

            try {

                Class clzz = Class.forName(className);

                Object o = clzz.getConstructor().newInstance();

                Method method = clzz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

                ajaxResult = (AjaxResult) method.invoke(o, req, resp);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            out.print(JSON.toJSONString(ajaxResult));


        }
    }
}
